package com.situ.mall.order.controller;

import com.situ.mall.common.entity.Member;
import com.situ.mall.common.service.IMemberService;
import com.situ.mall.common.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/")
public class LoginController {

    @Autowired
    private IMemberService memberService;
    @GetMapping("/login")
    private String index() {
        return "login2";
    }
    @PostMapping("/login")
    @ResponseBody
    public ResultVO loginCheck(HttpServletRequest request, Model model) {
        ResultVO vo=new ResultVO();
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println(username+","+password);
        Boolean right = memberService.check(username, password);
        System.out.println(right);
       Member member=memberService.selectByUsername(username);
       System.out.println("11111");
        vo.setData(member);
        System.out.println(member);
        //调用service检查是否正确
        if (right) {
            vo.setCode(1);
        } else {
            vo.setCode(0);
            vo.setMsg("用户名密码错误");
            //重定向到login
            model.addAttribute("msg", "用户名密码错误");
        }

        return vo;


    }
    @GetMapping("/relogin")
    public String relogin(){
        return "redirect:/login";
    }
}
